home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / src-glut / win32_x11.c < prev    next >
C/C++ Source or Header  |  1998-12-15  |  11KB  |  403 lines

  1.  
  2. /* Copyright (c) Nate Robins, 1997. */
  3. /* portions Copyright (c) Mark Kilgard, 1998. */
  4.  
  5. /* This program is freely distributable without licensing fees
  6.    and is provided without guarantee or warrantee expressed or
  7.    implied. This program is -not- in the public domain. */
  8.  
  9. #include <stdio.h>
  10. #include "win32_x11.h"
  11.  
  12. /* global variables that must be set for some functions to operate
  13.    correctly. */
  14. HDC XHDC;
  15. HWND XHWND;
  16.  
  17. XVisualInfo*
  18. XGetVisualInfo(Display* display, long mask, XVisualInfo* template, int* nitems)
  19. {
  20.   /* KLUDGE: this function needs XHDC to be set to the HDC currently
  21.      being operated on before it is invoked! */
  22.  
  23.   PIXELFORMATDESCRIPTOR* pfds;
  24.   int i, n;
  25.  
  26.   n = DescribePixelFormat(XHDC, 0, 0, NULL);
  27.   pfds = (PIXELFORMATDESCRIPTOR*)malloc(sizeof(PIXELFORMATDESCRIPTOR) * n);
  28.   memset(pfds, 0, sizeof(PIXELFORMATDESCRIPTOR) * n);
  29.  
  30.   for (i = 0; i < n; i++) {
  31.     DescribePixelFormat(XHDC, i, sizeof(PIXELFORMATDESCRIPTOR), &pfds[i]);
  32.   }
  33.  
  34.   *nitems = n;
  35.   return pfds;
  36. }
  37.  
  38. Colormap
  39. XCreateColormap(Display* display, Window root, Visual* visual, int alloc)
  40. {
  41.   /* KLUDGE: this function needs XHDC to be set to the HDC currently
  42.      being operated on before it is invoked! */
  43.  
  44.   PIXELFORMATDESCRIPTOR pfd;
  45.   LOGPALETTE *logical;
  46.   HPALETTE    palette;
  47.   int n;
  48.  
  49.   /* grab the pixel format */
  50.   memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  51.   DescribePixelFormat(XHDC, GetPixelFormat(XHDC),
  52.               sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  53.  
  54.   if (!(pfd.dwFlags & PFD_NEED_PALETTE ||
  55.       pfd.iPixelType == PFD_TYPE_COLORINDEX))
  56.   {
  57.     return 0;
  58.   }
  59.  
  60.   n = 1 << pfd.cColorBits;
  61.  
  62.   /* allocate a bunch of memory for the logical palette (assume 256
  63.      colors in a Win32 palette */
  64.   logical = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) +
  65.                 sizeof(PALETTEENTRY) * n);
  66.   memset(logical, 0, sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * n);
  67.  
  68.   /* set the entries in the logical palette */
  69.   logical->palVersion = 0x300;
  70.   logical->palNumEntries = n;
  71.  
  72.   /* start with a copy of the current system palette */
  73.   GetSystemPaletteEntries(XHDC, 0, 256, &logical->palPalEntry[0]);
  74.  
  75.   if (pfd.iPixelType == PFD_TYPE_RGBA) {
  76.     int redMask = (1 << pfd.cRedBits) - 1;
  77.     int greenMask = (1 << pfd.cGreenBits) - 1;
  78.     int blueMask = (1 << pfd.cBlueBits) - 1;
  79.     int i;
  80.  
  81.     /* fill in an RGBA color palette */
  82.     for (i = 0; i < n; ++i) {
  83.       logical->palPalEntry[i].peRed =
  84.     (((i >> pfd.cRedShift)   & redMask)   * 255) / redMask;
  85.       logical->palPalEntry[i].peGreen =
  86.     (((i >> pfd.cGreenShift) & greenMask) * 255) / greenMask;
  87.     logical->palPalEntry[i].peBlue =
  88.     (((i >> pfd.cBlueShift)  & blueMask)  * 255) / blueMask;
  89.       logical->palPalEntry[i].peFlags = 0;
  90.     }
  91.   }
  92.  
  93.   palette = CreatePalette(logical);
  94.   free(logical);
  95.  
  96.   SelectPalette(XHDC, palette, FALSE);
  97.   RealizePalette(XHDC);
  98.  
  99.   return palette;
  100. }
  101.  
  102. void
  103. XAllocColorCells(Display* display, Colormap colormap, Bool contig,
  104.          unsigned long plane_masks_return[], unsigned int nplanes,
  105.          unsigned long pixels_return[], unsigned int npixels)
  106. {
  107.   /* NOP -- we did all the allocating in XCreateColormap! */
  108. }
  109.  
  110. void
  111. XStoreColor(Display* display, Colormap colormap, XColor* color)
  112. {
  113.   /* KLUDGE: set XHDC to 0 if the palette should NOT be realized after
  114.      setting the color.  set XHDC to the correct HDC if it should. */
  115.  
  116.   PALETTEENTRY pe;
  117.  
  118.   /* X11 stores color from 0-65535, Win32 expects them to be 0-256, so
  119.      twiddle the bits ( / 256). */
  120.   pe.peRed = color->red / 256;
  121.   pe.peGreen = color->green / 256;
  122.   pe.peBlue = color->blue / 256;
  123.  
  124.   /* make sure we use this flag, otherwise the colors might get mapped
  125.      to another place in the colormap, and when we glIndex() that
  126.      color, it may have moved (argh!!) */
  127.   pe.peFlags = PC_NOCOLLAPSE;
  128.  
  129.   /* the pixel field of the XColor structure is the index into the
  130.      colormap */
  131.   SetPaletteEntries(colormap, color->pixel, 1, &pe);
  132.  
  133.   if (XHDC) {
  134.     UnrealizeObject(colormap);
  135.     SelectPalette(XHDC, colormap, FALSE);
  136.     RealizePalette(XHDC);
  137.   }
  138. }
  139.  
  140. void
  141. XSetWindowColormap(Display* display, Window window, Colormap colormap)
  142. {
  143.   HDC hdc = GetDC(window);
  144.  
  145.   /* if the third parameter is FALSE, the logical colormap is copied
  146.      into the device palette when the application is in the
  147.      foreground, if it is TRUE, the colors are mapped into the current
  148.      palette in the best possible way. */
  149.   SelectPalette(hdc, colormap, FALSE);
  150.   RealizePalette(hdc);
  151.  
  152.   /* note that we don't have to release the DC, since our window class
  153.      uses the WC_OWNDC flag! */
  154. }
  155.  
  156. Bool
  157. XTranslateCoordinates(Display *display, Window src, Window dst,
  158.               int src_x, int src_y,
  159.               int* dest_x_return, int* dest_y_return,
  160.               Window* child_return)
  161. {
  162.   /* KLUDGE: this isn't really a translate coordinates into some other
  163.   windows coordinate system...it only translates coordinates into the
  164.   root window (screen) coordinate system. */
  165.  
  166.   POINT point;
  167.  
  168.   point.x = src_x;
  169.   point.y = src_y;
  170.  
  171.   ClientToScreen(src, &point);
  172.  
  173.   *dest_x_return = point.x;
  174.   *dest_y_return = point.y;
  175.  
  176.   /* just to make compilers happy...we don't use the return value. */
  177.   return True;
  178. }
  179.  
  180. Status
  181. XGetGeometry(Display* display, Window window, Window* root_return,
  182.          int* x_return, int* y_return,
  183.          unsigned int* width_return, unsigned int* height_return,
  184.          unsigned int *border_width_return, unsigned int* depth_return)
  185. {
  186.   /* KLUDGE: doesn't return the border_width or depth or root, x & y
  187.      are in screen coordinates. */
  188.  
  189.   RECT rect;
  190.   POINT point;
  191.  
  192.   GetClientRect(window, &rect);
  193.  
  194.   point.x = 0;
  195.   point.y = 0;
  196.   ClientToScreen(window, &point);
  197.  
  198.   *x_return = point.x;
  199.   *y_return = point.y;
  200.   *width_return = rect.right;
  201.   *height_return = rect.bottom;
  202.  
  203.   /* just to make compilers happy...we don't use the return value. */
  204.   return 1;
  205. }
  206.  
  207. int
  208. DisplayWidthMM(Display* display, int screen)
  209. {
  210.   int width;
  211.   HWND hwnd = GetDesktopWindow();
  212.   HDC hdc = GetDC(hwnd);
  213.  
  214.   width = GetDeviceCaps(hdc, HORZSIZE);
  215.  
  216.   /* make sure to release this DC (it's the desktops, not ours) */
  217.   ReleaseDC(hwnd, hdc);
  218.  
  219.   return width;
  220. }
  221.  
  222. int
  223. DisplayHeightMM(Display* display, int screen)
  224. {
  225.   int height;
  226.   HWND hwnd = GetDesktopWindow();
  227.   HDC hdc = GetDC(hwnd);
  228.  
  229.   height = GetDeviceCaps(hdc, VERTSIZE);
  230.  
  231.   /* make sure to release this DC (it's the desktops, not ours) */
  232.   ReleaseDC(hwnd, hdc);
  233.  
  234.   return height;
  235. }
  236.  
  237. void
  238. XWarpPointer(Display* display, Window src, Window dst,
  239.          int src_x, int src_y, int src_width, int src_height,
  240.          int dst_x, int dst_y)
  241. {
  242.   /* KLUDGE: this isn't really a warp pointer into some other windows
  243.   coordinate system...it only warps the pointer into the root window
  244.   (screen) coordinate system. */
  245.  
  246.   POINT point;
  247.  
  248.   point.x = dst_x;
  249.   point.y = dst_y;
  250.   ClientToScreen(dst, &point);
  251.  
  252.   SetCursorPos(point.x, point.y);
  253. }
  254.  
  255. int
  256. XPending(Display* display)
  257. {
  258.   /* similar functionality...I don't think that it is exact, but this
  259.      will have to do. */
  260.   MSG msg;
  261.  
  262.   return PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
  263. }
  264.  
  265. /* the following function was stolen from the X sources as indicated. */
  266.  
  267. /* Copyright     Massachusetts Institute of Technology  1985, 1986, 1987 */
  268. /* $XConsortium: XParseGeom.c,v 11.18 91/02/21 17:23:05 rws Exp $ */
  269.  
  270. /*
  271. Permission to use, copy, modify, distribute, and sell this software and its
  272. documentation for any purpose is hereby granted without fee, provided that
  273. the above copyright notice appear in all copies and that both that
  274. copyright notice and this permission notice appear in supporting
  275. documentation, and that the name of M.I.T. not be used in advertising or
  276. publicity pertaining to distribution of the software without specific,
  277. written prior permission.  M.I.T. makes no representations about the
  278. suitability of this software for any purpose.  It is provided "as is"
  279. without express or implied warranty.
  280. */
  281.  
  282. /*
  283.  *    XParseGeometry parses strings of the form
  284.  *   "=<width>x<height>{+-}<xoffset>{+-}<yoffset>", where
  285.  *   width, height, xoffset, and yoffset are unsigned integers.
  286.  *   Example:  "=80x24+300-49"
  287.  *   The equal sign is optional.
  288.  *   It returns a bitmask that indicates which of the four values
  289.  *   were actually found in the string.  For each value found,
  290.  *   the corresponding argument is updated;  for each value
  291.  *   not found, the corresponding argument is left unchanged.
  292.  */
  293.  
  294. static int
  295. ReadInteger(char *string, char **NextString)
  296. {
  297.     register int Result = 0;
  298.     int Sign = 1;
  299.  
  300.     if (*string == '+')
  301.     string++;
  302.     else if (*string == '-')
  303.     {
  304.     string++;
  305.     Sign = -1;
  306.     }
  307.     for (; (*string >= '0') && (*string <= '9'); string++)
  308.     {
  309.     Result = (Result * 10) + (*string - '0');
  310.     }
  311.     *NextString = string;
  312.     if (Sign >= 0)
  313.     return (Result);
  314.     else
  315.     return (-Result);
  316. }
  317.  
  318. int XParseGeometry(char *string, int *x, int *y, unsigned int *width, unsigned int *height)
  319. {
  320.     int mask = NoValue;
  321.     register char *strind;
  322.     unsigned int tempWidth, tempHeight;
  323.     int tempX, tempY;
  324.     char *nextCharacter;
  325.  
  326.     if ( (string == NULL) || (*string == '\0')) return(mask);
  327.     if (*string == '=')
  328.         string++;  /* ignore possible '=' at beg of geometry spec */
  329.  
  330.     strind = (char *)string;
  331.     if (*strind != '+' && *strind != '-' && *strind != 'x') {
  332.         tempWidth = ReadInteger(strind, &nextCharacter);
  333.         if (strind == nextCharacter)
  334.             return (0);
  335.         strind = nextCharacter;
  336.         mask |= WidthValue;
  337.     }
  338.  
  339.     if (*strind == 'x' || *strind == 'X') {
  340.         strind++;
  341.         tempHeight = ReadInteger(strind, &nextCharacter);
  342.         if (strind == nextCharacter)
  343.             return (0);
  344.         strind = nextCharacter;
  345.         mask |= HeightValue;
  346.     }
  347.  
  348.     if ((*strind == '+') || (*strind == '-')) {
  349.         if (*strind == '-') {
  350.               strind++;
  351.             tempX = -ReadInteger(strind, &nextCharacter);
  352.             if (strind == nextCharacter)
  353.                 return (0);
  354.             strind = nextCharacter;
  355.             mask |= XNegative;
  356.  
  357.         }
  358.         else
  359.         {    strind++;
  360.             tempX = ReadInteger(strind, &nextCharacter);
  361.             if (strind == nextCharacter)
  362.                 return(0);
  363.             strind = nextCharacter;
  364.         }
  365.         mask |= XValue;
  366.         if ((*strind == '+') || (*strind == '-')) {
  367.             if (*strind == '-') {
  368.                 strind++;
  369.                 tempY = -ReadInteger(strind, &nextCharacter);
  370.                 if (strind == nextCharacter)
  371.                         return(0);
  372.                 strind = nextCharacter;
  373.                 mask |= YNegative;
  374.  
  375.             }
  376.             else
  377.             {
  378.                 strind++;
  379.                 tempY = ReadInteger(strind, &nextCharacter);
  380.                 if (strind == nextCharacter)
  381.                         return(0);
  382.                 strind = nextCharacter;
  383.             }
  384.             mask |= YValue;
  385.         }
  386.     }
  387.  
  388.     /* If strind isn't at the end of the string the it's an invalid
  389.         geometry specification. */
  390.  
  391.     if (*strind != '\0') return (0);
  392.  
  393.     if (mask & XValue)
  394.         *x = tempX;
  395.      if (mask & YValue)
  396.         *y = tempY;
  397.     if (mask & WidthValue)
  398.             *width = tempWidth;
  399.     if (mask & HeightValue)
  400.             *height = tempHeight;
  401.     return (mask);
  402. }
  403.